package com.common.widget;

import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.AbstractWheelTextAdapter;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.starappallcommon.R;

/**
 *  数字选择控件
 * 
 * <br>Created 2014-6-20 下午4:52:21
 * @version  
 * @author   chenyw		
 *
 * @see
 */
public class WheelNumberPicker extends LinearLayout implements OnWheelScrollListener{
	/** 上下文 */
	private Context mContext;

	/** 数字滚轮 */
	private WheelView wvNum;

	/** 滚轮适配器 */
	private WeekNumAdapter mWeekNumAdapter;

	/** 文字大小 */
	private final int TEXT_SIZE = 18;
	/** 当前选中Item的字体颜色 */
	private static final int CUR_COLOR = 0xFF000000;
	/** 默认Item的字体颜色 */
	private static final int DEF_COLOR = 0xFFC1C1C1;
	/** 当前位置值 */
	private int currItemValue = 0;
	/** 默认最小值 */
	private int minIndex = 0;
	/** 默认最大值 */
	private static final int DEFAULT_MAX = 999;
	private int maxIndex = DEFAULT_MAX;
	
	/**
	 * 初始化日期滚轮控件
	 * 
	 * @param context
	 *            上下文
	 */
	public WheelNumberPicker(Context context) {
		this(context, null);
	}

	/**
	 * 
	 * 初始化数字滚轮控件
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 */
	public WheelNumberPicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		// 显示的item数量
		final int visibleItemCount = 5;

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.wheel_number_picker, this, true);

		wvNum = (WheelView) findViewById(R.id.wvNum);
		wvNum.setVisibleItems(visibleItemCount);
		wvNum.addScrollingListener(this);
		wvNum.setCyclic(true);
		initData();
	}

	/**
	 *  初始化数据
	 * 
	 * <br>Created 2014-6-20 下午4:51:54
	 * @author        :  chenyw
	 */
	private void initData() {
		if (mWeekNumAdapter == null) {
			mWeekNumAdapter = new WeekNumAdapter(mContext, minIndex , maxIndex);
		} 
		wvNum.setViewAdapter(mWeekNumAdapter);
		wvNum.setCurrentItem(currItemValue);
	}

	/**
	 *   设为当前Item值
	 * 
	 * <br>Created 2014-6-20 下午4:51:43
	 * @param value  当前Item值
	 * @author        :  chenyw
	 */
	public void setCurrentItem(int value) {
	    currItemValue = value;
	}
	
	/**
	 *  获取当前item值
	 * 
	 * <br>Created 2014-6-20 下午5:06:37
	 * @return  当前item值
	 * @author        :  chenyw
	 */
	public int getCurrentItemValue(){
	    return currItemValue;
	}
	
	/**
	 *  设置显示的数字值区间
	 * 
	 * <br>Created 2014-6-20 下午4:54:14
	 * @param min   最小值
	 * @param max  最大值
	 * @author        :  chenyw
	 */
	public void setValueSection(final int min, final int max){
	    minIndex = min;
	    maxIndex = max;
	}

	/**
	 *  更新数字滚动滚轮的数据
	 * 
	 * <br>Created 2014-6-20 下午4:49:09
	 * @author        :  chenyw
	 */
	public void updateNum() {
	    initData();
	}

	/**
	 *  数字列表适配器
	 * 
	 * <br>Created 2014-6-20 下午4:52:42
	 * @version  WheelNumberPicker
	 * @author   chenyw		
	 *
	 * @see
	 */
	private class WeekNumAdapter extends AbstractWheelTextAdapter {
		// Count of days to be shown
	    private int minValue;
	    private int maxValue;
	    private String format = "%d";
	    
		/**
		 *  初始化
		 * Creates a new instance of WeekNumAdapter.
		 * <br>Created 2014-7-16 下午2:11:23
		 * @param context 上下文
		 * @param min 最小值
		 * @param max 最大值
		 */
		protected WeekNumAdapter(Context context, int min, int max) {
			super(context, R.layout.day_week, NO_RESOURCE);
			setTextSize(TEXT_SIZE);
			minValue = min;
			maxValue = max;
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {

			View view = super.getItem(index, cachedView, parent);
			Holder holder;
			if (view.getTag() == null) {
				holder = new Holder();
				holder.monthday = (TextView) view.findViewById(R.id.monthday);
				holder.weekday = (TextView) view.findViewById(R.id.weekday);
				holder.weekday.setTextSize(TEXT_SIZE);
				holder.weekday.setTextColor(DEF_COLOR);
			} else {
				holder = (Holder) view.getTag();
			}
			
			if(index == currItemValue){
			    holder.weekday.setTextColor(CUR_COLOR);
			}
			
			holder.weekday.setText(getItemText(index));
			holder.monthday.setVisibility(View.GONE);
			return view;
		}

		@Override
	    public CharSequence getItemText(int index) {
	        if (index >= 0 && index < getItemsCount()) {
	            int value = minValue + index;
	            if(format != null ){
	                return String.format(format, value);
	            }else{
	                Integer.toString(value);
	            }
	        }
	        return null;
	    }

	    @Override
	    public int getItemsCount() {
	        return maxValue - minValue + 1;
	    }    
	    
	    /**
	     *  通知刷新
	     * 
	     * <br>Created 2014-7-16 下午4:41:45
	     * @author        :  chenyw
	     */
	    public void refresh(){
	        notifyDataChangedEvent();
	    }

		/**
		 *  item Holder
		 * 
		 * <br>Created 2014-6-20 下午4:52:55
		 * @version  WheelNumberPicker.WeekNumAdapter
		 * @author   chenyw		
		 *
		 * @see
		 */
		private class Holder {
			public TextView monthday;
			public TextView weekday;
		}

		@Override
		protected CharSequence getCurItemText(int index) {
			return null;
		}
	}

    @Override
    public void onScrollingStarted(WheelView wheel) {
        
    }

    @Override
    public void onScrollingFinished(WheelView wheel) {
        currItemValue = wheel.getCurrentItem();
        wvNum.setCurrentItem(currItemValue);
        mWeekNumAdapter.refresh();
    }
}
